package org.wsocket.container;

import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import org.wsocket.Exception.WSocketException;
import org.wsocket.connector.Connector;
import org.wsocket.message.Message;
import org.wsocket.thread.SendMessageTask;
import org.wsocket.thread.ThreadPool;

public class Container {
	
	private ConcurrentHashMap<String, Connector> connectorMap = new ConcurrentHashMap<String, Connector>();
	private ConcurrentHashMap<Long, Container> containerMap = new ConcurrentHashMap<Long, Container>();
	
	private String containerName;
	private Long key;
	
	public String getContainerName() {
		return containerName;
	}

	public void setContainerName(String containerName) {
		this.containerName = containerName;
	}
	
	public Container(){
		this.key = System.currentTimeMillis();
	}
	
	public Long getKey(){
		return this.key;
	}
    
    /*
     * if resultList.size()>0,then the container has duplicated container in children contains.
     */
    public void checkIfContainsMe(List childContainList){
        if(childContainList.size()>0) 
        {
            //It has duplicated container,it doesn't need do the follow check
            return;
        }
        Iterator<Long> i = containerMap.keySet().iterator();
        while(i.hasNext()){
            Long k = (Long)i.next();
            if(key.equals(k))
            {
                childContainList.add(k);
            }
            else
            {
                Container childContainer = containerMap.get(k);
                childContainer.checkIfContainsMe(childContainList);
            }
        }   
    }

	public Long addContainer(Container container){
		containerMap.put(container.getKey(), container);
		return container.getKey();
	}
	
	public String addConnector(Connector connector){
		connectorMap.put(connector.getKey(), connector);
		return connector.getKey();
	}
	
	public Long removeContainer(Container container){
		containerMap.remove(container.getKey());
		return container.getKey();
	}
	
	public String removeConnector(Connector connector){
		connectorMap.remove(connector.getKey());
		return connector.getKey();
	}
	
	public void broadcast(String message) throws WSocketException{
		this.send2AllConnector(message);
		this.send2AllContainer(message);
	}
	
	public void send2AllConnector(String message) throws WSocketException{
		Iterator<String> i = connectorMap.keySet().iterator();
		while(i.hasNext()){
			String k = i.next();
			Connector target = connectorMap.get(k);
			SendMessageTask task = new SendMessageTask(new Message(null, target,
					message));
			ThreadPool.assginTask(task);
		}
	}
	
	public void send2AllContainer(String message) throws WSocketException{
		Iterator<Long> i = containerMap.keySet().iterator();
		while(i.hasNext()){
			Long k = (Long)i.next();
			Container target = containerMap.get(k);
			target.broadcast(message);
		}
	}
	
	
	
	
}
